package com.code;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import com.baomidou.mybatisplus.annotation.IdType;

import java.util.Collections;

/**
 * 简化版自定义模板代码生成器测试
 */
public class SimpleCustomGenerator {

    public static void main(String[] args) {
        // 设置系统编码为UTF-8
        System.setProperty("file.encoding", "UTF-8");
        System.setProperty("sun.jnu.encoding", "UTF-8");
        
        System.out.println("==================== 简化版自定义模板测试 ====================");
        System.out.println("当前文件编码: " + System.getProperty("file.encoding"));
        System.out.println("JNU编码: " + System.getProperty("sun.jnu.encoding"));
        
        try {
            // 先删除原有的文件，使用简单的实体类模板进行测试
            String url = "jdbc:dm://192.168.170.182:5236/YTHDB";
            String username = "sysdba";
            String password = "Ancc#2025";
            
            String projectPath = System.getProperty("user.dir");
            String outputDir = projectPath + "/generated";
            String xmlOutputDir = projectPath + "/generated/mapper";
            
            System.out.println("输出到测试目录: " + outputDir);
            
            // 使用默认模板但增强配置
            FastAutoGenerator.create(url, username, password)
                    .globalConfig(builder -> {
                        builder.author("CustomGenerator")
                                .outputDir(outputDir)
                                .dateType(DateType.TIME_PACK)
                                .commentDate("yyyy-MM-dd HH:mm:ss")
                                .enableSwagger()
                                .disableOpenDir();
                    })
                    .packageConfig(builder -> {
                        builder.parent("com.example.demo")
                                .moduleName("crud")
                                .entity("entity")
                                .mapper("mapper")
                                .service("service")
                                .serviceImpl("service.impl")
                                .controller("controller")
                                .pathInfo(Collections.singletonMap(OutputFile.xml, xmlOutputDir));
                    })
                    .strategyConfig(builder -> {
                        builder.addInclude("YTH_SYS_PERSON")
                                // 配置表前缀去除
                                .addTablePrefix("YTH_", "sys_", "t_")
                                .entityBuilder()
                                .enableLombok()
                                .enableTableFieldAnnotation()
                                .enableActiveRecord()
                                .enableChainModel()
                                .naming(NamingStrategy.underline_to_camel)
                                .columnNaming(NamingStrategy.underline_to_camel)
                                .idType(IdType.AUTO)
                                .formatFileName("%sEntity")
                                .enableFileOverride()
                                
                                .mapperBuilder()
                                .superClass(BaseMapper.class)
                                .enableMapperAnnotation()
                                .enableBaseResultMap()
                                .enableBaseColumnList()
                                .formatMapperFileName("%sMapper")
                                .formatXmlFileName("%sMapper")
                                .enableFileOverride()
                                
                                .serviceBuilder()
                                .formatServiceFileName("%sService")
                                .formatServiceImplFileName("%sServiceImpl")
                                .enableFileOverride()
                                
                                .controllerBuilder()
                                .enableRestStyle()
                                .enableHyphenStyle()
                                .formatFileName("%sController")
                                .enableFileOverride();
                    })
                    .templateEngine(new FreemarkerTemplateEngine())
                    .execute();
            
            System.out.println("==================== 测试生成完成 ====================");
            
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}